﻿/**
* ...
* @author Donald Martinez
* @version 0.1
*/
/*
 * 
 */
package bdz.common.display{
	import bdz.common.events.SliderEvent;
	import bdz.common.utils.BitmapUtils;
	import bdz.common.utils.TweenLite;
	
	import fl.transitions.easing.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.geom.Point;
	import flash.utils.*;
	
	public class MCScroller extends Slider {
		private var scrollTimeout;
		private var targ:Sprite;
		private var masker:MovieClip;
		private var _automask:Boolean = false;
		private var origpos:Number;
		public var offset:Number = 0;
		private var eased:Boolean = true;
		private var _easing:Function = Regular.easeOut;
		private var espeed:Number = .9;
		private var _currentmark:Number;
		function MCScroller(){
			super();
			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE, cleanUp);

		}
		private function cleanUp(evt:Event):void{
			targ.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
		}
		private function addedToStage(evt:Event):void{
			masker = new MovieClip();
			parent.addChild(masker);
		}
		private function createMask():void{
			
			BitmapUtils.createBounds(masker, targ.width+5, height, 1, 0x000000);
			var targloc:Point = new Point(targ.x, targ.y);
			
			masker.x = targ.x;
			masker.y = targ.y;

			targ.mask = masker;
			
		}
		private function initScroller():void{
			targ.cacheAsBitmap = true;
			origpos = targ[pos];
			addEventListener(SliderEvent.SCROLL, checkScrolling);
			targ.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
		}
		private function onMouseWheel(evt:MouseEvent):void{
			if (evt.delta>0) {
				scrollUp();
			} else {
				scrollDown();
			}
		}
		
		private function checkScrolling(evt:SliderEvent):void{
			
			if(!eased){
				targ[pos] = origpos-svalue;
				_currentmark = origpos-svalue;
			}else{
				var o:Object = new Object();
				o[pos] = origpos-svalue;
				o.ease = _easing;
				o.overwrite = true;
				TweenLite.to(targ, espeed, o);
			}
			clearTimeout(scrollTimeout);
			scrollTimeout = setTimeout(stoppedScroll, 1000);
			
		}
		private function stoppedScroll():void{
			dispatchEvent(new SliderEvent(SliderEvent.STOPPED_SCROLL, null));
		}
		private function setMaxScroll():void{
			if((targ[prop]+offset)-height>0){
				maxScroll = Math.floor((targ[prop]+offset)-height);
			}
			
		}
		public function set scrollTarget(mc:Sprite):void{
			targ = mc;
			setMaxScroll();
			if(_automask){
				createMask();
			}
			
			initScroller();
			visible = _maxscroll > 0;
		}
		public function forceOrigPos(n:Number):void{
			origpos = n;
		}
		public function get scrollTarget():Sprite{
			return targ;
		}
		public function set autoMask(b:Boolean):void{
			_automask = b;
		}
		public function get autoMask():Boolean{
			return _automask;
		}
		public function set easing(e:Function):void{
			_easing = e;
		}
		public function get easing():Function{
			return _easing;
		}
		public function set easeSpeed(n:Number):void{
			espeed = n;
		}
		public function get easeSpeed():Number{
			return espeed;
		}
		public function set ease(b:Boolean):void{
			eased = b;
		}
		public function get ease():Boolean{
			return eased;
		}
	}
	
}